import { PlatformPermissions } from './platformPermissions';

/**
 * 平台菜单配置
 * 定义菜单结构、权限要求和显示逻辑
 */
export interface PlatformMenuItem {
  key: string;
  icon: string; // 图标名称
  label: string;
  path: string;
  requiredPermissions?: string[];
  requiredRoles?: string[];
  children?: PlatformMenuItem[];
  hidden?: boolean;
  order?: number;
}

/**
 * 平台菜单配置
 */
export const PLATFORM_MENU_CONFIG: PlatformMenuItem[] = [
  {
    key: 'dashboard',
    icon: 'HomeOutlined',
    label: '平台概览',
    path: '/platform/dashboard',
    order: 1,
  },
  {
    key: 'tenant-management',
    icon: 'ShopOutlined',
    label: '租户管理',
    path: '/platform/tenants',
    requiredPermissions: [PlatformPermissions.TENANT_READ],
    order: 2,
  },
  {
    key: 'user-management',
    icon: 'TeamOutlined',
    label: '平台用户',
    path: '/platform/users',
    requiredPermissions: [PlatformPermissions.PLATFORM_USER_READ],
    order: 3,
  },
  {
    key: 'role-management',
    icon: 'SafetyCertificateOutlined',
    label: '角色管理',
    path: '/platform/roles',
    requiredPermissions: [PlatformPermissions.PLATFORM_USER_READ],
    order: 4,
  },
  {
    key: 'permission-management',
    icon: 'KeyOutlined',
    label: '权限管理',
    path: '/platform/permissions',
    requiredPermissions: [PlatformPermissions.PLATFORM_USER_READ],
    order: 5,
  },
  {
    key: 'ticket-management',
    icon: 'CustomerServiceOutlined',
    label: '工单管理',
    path: '/platform/tickets',
    requiredPermissions: [PlatformPermissions.TICKET_READ],
    order: 6,
  },
];

/**
 * 用户下拉菜单配置
 */
export const USER_MENU_CONFIG: PlatformMenuItem[] = [
  {
    key: 'profile',
    icon: 'UserOutlined',
    label: '个人资料',
    path: '/platform/profile',
    order: 1,
  },
  {
    key: 'settings',
    icon: 'SettingOutlined',
    label: '系统设置',
    path: '/platform/settings',
    requiredPermissions: [PlatformPermissions.SYSTEM_CONFIG],
    order: 2,
  },
];

/**
 * 菜单权限检查函数
 */
export const checkMenuPermission = (
  menuItem: PlatformMenuItem,
  userPermissions: string[],
  userRoles: string[],
  isSuperAdmin: boolean
): boolean => {
  // 超级管理员可以访问所有菜单
  if (isSuperAdmin) {
    return true;
  }

  // 检查权限要求
  if (menuItem.requiredPermissions && menuItem.requiredPermissions.length > 0) {
    const hasPermission = menuItem.requiredPermissions.some(permission => {
      // 检查精确权限
      if (userPermissions.includes(permission)) {
        return true;
      }
      
      // 检查通配符权限
      return userPermissions.some(userPerm => {
        if (userPerm.endsWith(':*')) {
          const prefix = userPerm.substring(0, userPerm.length - 1);
          return permission.startsWith(prefix);
        }
        return false;
      });
    });
    
    if (!hasPermission) {
      return false;
    }
  }

  // 检查角色要求
  if (menuItem.requiredRoles && menuItem.requiredRoles.length > 0) {
    const hasRole = menuItem.requiredRoles.some(role => userRoles.includes(role));
    if (!hasRole) {
      return false;
    }
  }

  return true;
};

/**
 * 过滤有权限的菜单项
 */
export const filterMenuByPermissions = (
  menuConfig: PlatformMenuItem[],
  userPermissions: string[],
  userRoles: string[],
  isSuperAdmin: boolean
): PlatformMenuItem[] => {
  return menuConfig
    .filter(item => !item.hidden && checkMenuPermission(item, userPermissions, userRoles, isSuperAdmin))
    .sort((a, b) => (a.order || 999) - (b.order || 999));
};